//
//  OrderOnlineViewController.m
//  OrderOnline
//
//  Created by YAPPA_3 on 11/28/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "OrderOnlineViewController.h"
#import <QuartzCore/QuartzCore.h>
#define VIEW_ANIM_DURATION_NORMAL		0.3f

@implementation OrderOnlineViewController
@synthesize m_isSlideShow;
@synthesize m_typeMenu;
static OrderOnlineViewController * gInstance = nil;

- (id)init
{
	if( self = [super init] )
	{
		m_prevViewType = eVMTypeInvalid;
		m_nowViewType  = eVMTypeInvalid;
		m_isSlideShow = NO;
		m_typeMenu = TYPE_MENU_ALL;
	}
	return self;
}

-(void)saveType:(eVMType)type
{
	m_prevViewType = m_nowViewType;
	m_nowViewType  = type;
	[self removeViewObject:m_prevViewType];
	
}

-(void)replaceView:(UIView*)view isAnim:(BOOL)is_anim
{
	if( !view )
		return;
	
	if (m_prevViewType == eVMTypeReadView) {
		
		replaceViewCAAnim( self.m_BaseView, view, m_currentView, is_anim, kCATransitionPush, kCATransitionFromLeft, VIEW_ANIM_DURATION_NORMAL);
	}
	if (m_prevViewType == eVMTypeOrderView) {
		
		replaceViewCAAnim( self.m_BaseView, view, m_currentView, is_anim, kCATransitionPush, kCATransitionFromLeft, VIEW_ANIM_DURATION_NORMAL);
	}
	else {
		replaceViewUIViewAnim( self.m_BaseView, view, m_currentView,
							  is_anim, UIViewAnimationTransitionFlipFromRight,
							  VIEW_ANIM_DURATION_NORMAL
							  );
	}

			
	m_currentView = view;
}

-(void)removeViewObject:(eVMType)type
{
	switch( type )
	{
		case eVMTypeSplash:
			
			break;
		case eVMTypeTitle:
			SAFE_RELEASE( m_titleManager );	
			break;
		case eVMTypeHome:
			SAFE_RELEASE( m_homeManager );	
			break;
		case eVMTypeReadView:
			SAFE_RELEASE( m_readManager );	
			break;
		case eVMTypeOrderView:
			SAFE_RELEASE( m_orderManager );	
			break;
			
	}
}

- (void)changePrevView:(BOOL)is_anim
{
	switch( m_prevViewType )
	{
		case eVMTypeHome:
		{		
			[self changeHomeView:is_anim];
			break;
		}	
		case eVMTypeReadView:
		{		
			[self changeReadView:is_anim];
			break;
		}		
	}
}
- (void)changeHomeView:(BOOL)is_anim
{
	[self saveType:eVMTypeHome];
	SAFE_RELEASE( m_homeManager );
	{
		CGRect disp_rect = getDispRectFromDevice();
		m_homeManager= [HomeViewManager alloc];
		[m_homeManager initWithFrame:disp_rect is_anim:YES];
	}	
	[self replaceView:m_homeManager isAnim:is_anim];
}

- (void)changeReadView:(BOOL)is_anim
{
	[self saveType:eVMTypeReadView];
	SAFE_RELEASE( m_readManager );
	{
		CGRect disp_rect = getDispRectFromDevice();
		m_readManager= [ReadViewManager alloc];
		[m_readManager initWithFrame:disp_rect is_anim:YES];
	}	
	[self replaceView:m_readManager isAnim:is_anim];
}
- (void) changePage:(int)pageNo
{	
	[m_readManager.m_readView changePage:pageNo];


}

- (void)changeTitleView:(BOOL)is_anim
{			
	[self saveType:eVMTypeTitle];
	SAFE_RELEASE( m_titleManager );
	{
		CGRect disp_rect = getDispRectFromDevice();
		m_titleManager = [TitleViewManager alloc];				
		[m_titleManager initWithFrame:disp_rect];
	}	
	[self replaceView:m_titleManager isAnim:is_anim];
	
	CGRect view_frame = m_titleManager.frame;
	view_frame.origin.y = (-CLOCK_HEIGHT/2.0f);
	m_titleManager.frame = view_frame;
	
	SAFE_RELEASE( m_titleManager );
}

- (void)changeOrderView:(BOOL)is_anim
{
	[self saveType:eVMTypeOrderView];
	SAFE_RELEASE( m_orderManager );
	{
		CGRect disp_rect = getDispRectFromDevice();
		m_orderManager= [OrderViewManager alloc];
		[m_orderManager initWithFrame:disp_rect is_anim:YES];
	}	
	[self replaceView:m_orderManager isAnim:is_anim];
}

- (void) refreshOrder
{
	[m_orderManager.m_orderView loadListThumb];
	[m_orderManager.m_orderView correctThumbFrame];
	[m_orderManager.m_orderView addThumbToScrollView];
}
- (void) setSlideShow: (BOOL) isSlideShow
{
	[m_readManager.m_readView slideShow: isSlideShow];
}

- (void)dealloc {
    [super dealloc];
	[m_homeManager release];
	[m_readManager release];
	[m_titleManager release];
}

- (void)update
{
	if( m_viewController )
	{
		if( [m_viewController respondsToSelector:@selector(update)] )
			[m_viewController update];
	}
	[super update];
}


+ (OrderOnlineViewController *)Shared{
	if (!gInstance) {
		gInstance = [[OrderOnlineViewController alloc] init];
	}
	return gInstance;
}


@end
